# Copyright (C) 2013-2015 Free Software Foundation, Inc.

# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with GCC; see the file COPYING3.  If not see
# <http://www.gnu.org/licenses/>.

# Return 1 if compilation with -fsanitize=thread is error-free for trivial
# code, 0 otherwise.

proc check_effective_target_fsanitize_thread {} {
    return [check_runtime fsanitize_thread {
	int main (void) { return 0; }
    } "-fsanitize=thread"]
}

#
# tsan_link_flags -- compute library path and flags to find libtsan.
# (originally from g++.exp)
#

proc tsan_link_flags { paths } {
    global srcdir
    global ld_library_path
    global shlib_ext
    global tsan_saved_library_path

    set gccpath ${paths}
    set flags ""

    set shlib_ext [get_shlib_extension]
    set tsan_saved_library_path $ld_library_path

    if { $gccpath != "" } {
      if { [file exists "${gccpath}/libsanitizer/tsan/.libs/libtsan.a"]
	   || [file exists "${gccpath}/libsanitizer/tsan/.libs/libtsan.${shlib_ext}"] } {
	  append flags " -B${gccpath}/libsanitizer/tsan/ "
	  append flags " -L${gccpath}/libsanitizer/tsan/.libs "
	  append ld_library_path ":${gccpath}/libsanitizer/tsan/.libs"
      }
    } else {
      global tool_root_dir

      set libtsan [lookfor_file ${tool_root_dir} libtsan]
      if { $libtsan != "" } {
	  append flags "-L${libtsan} "
	  append ld_library_path ":${libtsan}"
      }
    }

    set_ld_library_path_env_vars

    return "$flags"
}

#
# tsan_init -- called at the start of each subdir of tests
#

proc tsan_init { args } {
    global TEST_ALWAYS_FLAGS
    global ALWAYS_CXXFLAGS
    global TOOL_OPTIONS
    global tsan_saved_TEST_ALWAYS_FLAGS
    global tsan_saved_ALWAYS_CXXFLAGS
    global dg-do-what-default
    global tsan_saved_dg-do-what-default

    set link_flags ""
    if ![is_remote host] {
	if [info exists TOOL_OPTIONS] {
	    set link_flags "[tsan_link_flags [get_multilibs ${TOOL_OPTIONS}]]"
	} else {
	    set link_flags "[tsan_link_flags [get_multilibs]]"
	}
    }

    if [info exists dg-do-what-default] {
	set tsan_saved_dg-do-what-default ${dg-do-what-default}
    }
    if [info exists TEST_ALWAYS_FLAGS] {
	set tsan_saved_TEST_ALWAYS_FLAGS $TEST_ALWAYS_FLAGS
    }
    if [info exists ALWAYS_CXXFLAGS] {
	set tsan_saved_ALWAYS_CXXFLAGS $ALWAYS_CXXFLAGS
	set ALWAYS_CXXFLAGS [concat "{ldflags=$link_flags}" $ALWAYS_CXXFLAGS]
	set ALWAYS_CXXFLAGS [concat "{additional_flags=-fsanitize=thread -g}" $ALWAYS_CXXFLAGS]
    } else {
	if [info exists TEST_ALWAYS_FLAGS] {
	    set TEST_ALWAYS_FLAGS "$link_flags -fsanitize=thread -g $TEST_ALWAYS_FLAGS"
	} else {
	    set TEST_ALWAYS_FLAGS "$link_flags -fsanitize=thread -g"
	}
    }

    set dg-do-what-default run
    if { $link_flags != "" } {
	global individual_timeout

	# Lower timeout value in case test does not terminate properly.
	set individual_timeout 20
	if [check_runtime_nocache tsan_works {
		int main () { return 0; }
	    } "-fsanitize=thread -g"] {
	    set dg-do-what-default run
	} else {
	    set dg-do-what-default compile
	}
	unset individual_timeout
    }
}

#
# tsan_finish -- called at the end of each subdir of tests
#

proc tsan_finish { args } {
    global TEST_ALWAYS_FLAGS
    global tsan_saved_TEST_ALWAYS_FLAGS
    global tsan_saved_ALWAYS_CXXFLAGS
    global dg-do-what-default
    global tsan_saved_dg-do-what-default
    global tsan_saved_library_path
    global ld_library_path

    if [info exists tsan_saved_ALWAYS_CXXFLAGS ] {
	set ALWAYS_CXXFLAGS $tsan_saved_ALWAYS_CXXFLAGS
    } else {
	if [info exists tsan_saved_TEST_ALWAYS_FLAGS] {
	    set TEST_ALWAYS_FLAGS $tsan_saved_TEST_ALWAYS_FLAGS
        } else {
	    unset TEST_ALWAYS_FLAGS
        }
    }

    if [info exists tsan_saved_dg-do-what-default] {
	set dg-do-what-default ${tsan_saved_dg-do-what-default}
    } else {
	unset dg-do-what-default
    }
    set ld_library_path $tsan_saved_library_path
    set_ld_library_path_env_vars
}
